package com.baizhi.controller;

import com.baizhi.entity.Dept;
import com.baizhi.entity.Emp;
import com.baizhi.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("emp")
public class EmpController {

    @Autowired
    private EmpService empService;

    //分页查询
    @GetMapping("ShowAllEmp")
    public List<Emp> showAllEmp(@RequestParam(value = "pageNo",defaultValue = "1") int pageNo, HttpServletRequest request){
        if(pageNo == 0)pageNo=1;
        List<Emp> list = empService.queryPageEmp(pageNo);
        return list;
    }

    //总页数
    @GetMapping("totalpage")
    public int showAllpage(){
        int totalPage = empService.queryAllTotal();
        return totalPage;
    }

    //查询所有部门
    @GetMapping("depts")
    public List<Dept>showAllDept(){
        List<Dept>list =  empService.queryAllDept();
        return list;
    }

    //添加
    @PostMapping ("add")
    public void addEmp(@RequestBody Emp user){
        System.out.println("我打印出来了"+user);
        empService.addEmp(user,user.getDept().getName());
    }

    //修改
    @PostMapping("update")
    public void update(@RequestBody Emp emp){
        emp.setId(emp.getId());
        empService.updEmp(emp,emp.getDept().getName());
    }

    //删除
    @PostMapping("delete")
    public void delEmp(@RequestBody Emp emp){
        System.out.println(emp.getId());
        empService.delEmpbyId(emp.getId());
    }



}
